import {enableProdMode, ViewEncapsulation} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import {AppModule} from './app/app.module';
import {environment} from './environments/environment';

import {preloaderFinished} from '@delon/theme';

preloaderFinished();

// HMR:webpack热模块，模块热加载
import {hmrBootstrap} from './hmr';

if (environment.production) {
    enableProdMode();
    // prod模式下清除console,重写console对象下的方法
    window.console.log = function () {
    };
    window.console.info = function () {
    };
    window.console.warn = function () {
    };
    window.console.error = function () {
    };
    window.console.debug = function () {
    };
}

// Angular2+启动函数：使用给定的运行时编译器为给定平台(AppModule)创建`@NgModule`实例
const bootstrap = () => {
    return platformBrowserDynamic()
        .bootstrapModule(AppModule, {
            defaultEncapsulation: ViewEncapsulation.Emulated,
            preserveWhitespaces: false,
        })
        .then(res => {
            if ((<any>window).appBootstrap) {
                (<any>window).appBootstrap();
            }
            return res;
        });
};

if (environment.hmr) {
    if (module['hot']) {
        hmrBootstrap(module, bootstrap);
    } else {
        console.error('HMR is not enabled for webpack-dev-server!');
        console.log('Are you using the --hmr flag for ng serve?');
    }
} else {
    bootstrap();
}
